package com.study.talk.net;

import androidx.appcompat.app.AppCompatActivity;

import com.google.gson.reflect.TypeToken;
import com.study.talk.entity.GroupInfo;
import com.study.talk.entity.MomentComment;
import com.study.talk.entity.MomentLike;
import com.study.talk.entity.User;
import com.study.talk.entity.UserInfo;
import com.study.talk.entity.resp.GroupInfoResponse;
import com.study.talk.entity.resp.MomentResponse;
import com.study.talk.entity.resp.ResultResponse;
import com.study.talk.entity.resp.UploadFileResponse;
import com.study.talk.entity.resp.UserInfoResponse;
import com.study.talk.entity.vo.MomentCommentVo;
import com.study.talk.entity.vo.MomentLikeVo;
import com.study.talk.entity.vo.MomentVo;
import com.study.talk.entity.vo.UpdateLikeStateVo;
import com.study.talk.util.GsonInstance;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import top.zibin.luban.Luban;
import top.zibin.luban.OnCompressListener;

public class NetApi {
    public static void getUserInfo(Integer userId, RequestSuccessCallBack<UserInfo> successCallBack, RequestFailCallBack failCallBack) {
        OkHttp.get("/userInfo/get/" + userId, res -> {
            UserInfoResponse response = GsonInstance.instance.fromJson(res, UserInfoResponse.class);
            if (response.getStatus() == 200) {
                successCallBack.success(response.getData());
            } else {
                failCallBack.fail(response.getMessage());
            }
        });
    }

    public static void getGroupInfo(Integer groupId, RequestSuccessCallBack<GroupInfo> successCallBack, RequestFailCallBack failCallBack) {
        OkHttp.get("/groupInfo/" + groupId, res -> {
            GroupInfoResponse response = GsonInstance.instance.fromJson(res, GroupInfoResponse.class);
            if (response.getStatus() == 200) {
                successCallBack.success(response.getData());
            } else {
                failCallBack.fail(response.getMessage());
            }
        });
    }

    public static void uploadMultipleFileOther(List<String> files, AppCompatActivity activity, RequestSuccessCallBack<ArrayList<String>> successCallBack, RequestFailCallBack failCallBack) {
        try {
            List<File> fileList = Luban.with(activity)
                    .load(files)
                    .get();
            List<String> resultsList = new ArrayList<>();
            for (File file : fileList) {
                resultsList.add(file.getPath());
            }
            OkHttp.uploadFiles("/file/upload/image/multiple/other", resultsList, res -> {
                Type type = new TypeToken<ResultResponse<List<String>>>() {
                }.getType();
                ResultResponse<ArrayList<String>> response = GsonInstance.instance.fromJson(res, type);
                if (response.getStatus() == 200) {
                    successCallBack.success(response.getData());
                } else {
                    failCallBack.fail(response.getMessage());
                }
            });
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static void uploadMultipleFile(List<String> files, RequestSuccessCallBack<ArrayList<String>> successCallBack, RequestFailCallBack failCallBack) {
        OkHttp.uploadFiles("/file/upload/image/multiple", files, res -> {
            Type type = new TypeToken<ResultResponse<List<String>>>() {
            }.getType();
            ResultResponse<ArrayList<String>> response = GsonInstance.instance.fromJson(res, type);
            if (response.getStatus() == 200) {
                successCallBack.success(response.getData());
            } else {
                failCallBack.fail(response.getMessage());
            }
        });
    }

    public static void addMoment(UserInfo userInfo, ArrayList<String> photos, String content, RequestSuccessCallBack<Boolean> successCallBack, RequestFailCallBack failCallBack) {
        MomentVo momentVo = new MomentVo(userInfo.getId(), userInfo.getAvatar(), userInfo.getUsername(), photos, content);
        OkHttp.post("/moment/add", GsonInstance.instance.toJson(momentVo), res -> {
            ResultResponse<Object> response = GsonInstance.instance.fromJson(res, new TypeToken<ResultResponse<Object>>() {
            }.getType());
            if (response.getStatus() == 200) {
                successCallBack.success(true);
            } else {
                failCallBack.fail(response.getMessage());
            }
        });
    }

    public static void getUserMomentList(Integer userId, RequestSuccessCallBack<List<MomentResponse>> successCallBack, RequestFailCallBack failCallBack) {
        OkHttp.get("/moment/get/my/moment/" + userId, res -> {
            ResultResponse<List<MomentResponse>> response = GsonInstance.instance.fromJson(res, new TypeToken<ResultResponse<List<MomentResponse>>>() {
            }.getType());
            if (response.getStatus() == 200) {
                successCallBack.success(response.getData());
            } else {
                failCallBack.fail(response.getMessage());
            }
        });
    }

    //根据userId获取朋友圈列表
    public static void getFriendMomentList(Integer userId, RequestSuccessCallBack<List<MomentResponse>> successCallBack, RequestFailCallBack failCallBack) {
        OkHttp.get("/moment/get/friend/moment/" + userId, res -> {
            ResultResponse<List<MomentResponse>> response = GsonInstance.instance.fromJson(res, new TypeToken<ResultResponse<List<MomentResponse>>>() {
            }.getType());
            if (response.getStatus() == 200) {
                successCallBack.success(response.getData());
            } else {
                failCallBack.fail(response.getMessage());
            }
        });
    }


    public static void addMomentLike(User user, Integer momentId, Integer likeState, RequestSuccessCallBack<Boolean> successCallBack, RequestFailCallBack failCallBack) {
        MomentLikeVo momentLikeVo = new MomentLikeVo(momentId, user.getId(), user.getUsername(), likeState);
        OkHttp.post("/moment_like/add", GsonInstance.instance.toJson(momentLikeVo), res -> {
            ResultResponse response = GsonInstance.instance.fromJson(res, ResultResponse.class);
            if (response.getStatus() == 200) {
                successCallBack.success(true);
            } else {
                failCallBack.fail(response.getMessage());
            }
        });
    }

    public static void updateMomentLike(UpdateLikeStateVo updateLikeStateVo, RequestSuccessCallBack<Boolean> successCallBack, RequestFailCallBack failCallBack) {
        OkHttp.put("/moment_like/update/like_state", GsonInstance.instance.toJson(updateLikeStateVo), res -> {
            ResultResponse response = GsonInstance.instance.fromJson(res, ResultResponse.class);
            if (response.getStatus() == 200) {
                successCallBack.success(true);
            } else {
                failCallBack.fail(response.getMessage());
            }
        });
    }

    public static void addMomentComment(User user, Integer momentId, String content, RequestSuccessCallBack<Boolean> successCallBack, RequestFailCallBack failCallBack) {
        MomentCommentVo momentCommentVo = new MomentCommentVo(momentId, user.getId(), user.getUsername(), content);
        OkHttp.post("/moment_comment/add", GsonInstance.instance.toJson(momentCommentVo), res -> {
            ResultResponse response = GsonInstance.instance.fromJson(res, ResultResponse.class);
            if (response.getStatus() == 200) {
                successCallBack.success(true);
            } else {
                failCallBack.fail(response.getMessage());
            }
        });
    }

    public static void deleteMomentComment(MomentComment momentComment, RequestSuccessCallBack<Boolean> successCallBack, RequestFailCallBack failCallBack) {
        OkHttp.delete("/moment_comment/delete", GsonInstance.instance.toJson(momentComment), res -> {
            ResultResponse response = GsonInstance.instance.fromJson(res, ResultResponse.class);
            if (response.getStatus() == 200) {
                successCallBack.success(true);
            } else {
                failCallBack.fail(response.getMessage());
            }
        });
    }

    public static void getLikeList(Integer momentId, RequestSuccessCallBack<List<MomentLike>> successCallBack, RequestFailCallBack failCallBack) {
        OkHttp.get("/moment_like/get/list/" + momentId, res -> {
            ResultResponse<List<MomentLike>> response = GsonInstance.instance.fromJson(res, new TypeToken<ResultResponse<List<MomentLike>>>() {
            }.getType());
            if (response.getStatus() == 200) {
                successCallBack.success(response.getData());
            } else {
                failCallBack.fail(response.getMessage());
            }
        });
    }

    public static void uploadImage(String path, AppCompatActivity context, RequestSuccessCallBack<String> successCallBack, RequestFailCallBack failCallBack) {
        Luban.with(context)
                .load(path)
                .setCompressListener(new OnCompressListener() {
                    @Override
                    public void onStart() {

                    }

                    @Override
                    public void onSuccess(File file) {
                        OkHttp.postFile("/file/register/upload/image", file.getPath(), res -> {
                            UploadFileResponse response = GsonInstance.instance.fromJson(res, UploadFileResponse.class);
                            if (response.getStatus() == 200) {
                                successCallBack.success(response.getData());
                            } else {
                                failCallBack.fail(response.getMessage());
                            }
                        });
                    }

                    @Override
                    public void onError(Throwable e) {

                    }
                }).launch();

    }
}
